<template>
  <div id="UserRegisterView">
    <h2 style="margin-bottom: 16px">用户注册</h2>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      :model="form"
      @submit="handleSubmit"
      label-align="left"
      auto-label-width
    >
      <!-- 账号 -->
      <a-form-item field="userAccount" tooltip="请输入账号" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>

      <!-- 密码 -->
      <a-form-item field="userPassword" label="密码" tooltip="至少8位">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <!-- 确认密码 -->
      <a-form-item
        field="checkPassword"
        label="确认密码"
        tooltip="再次输入密码"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请再次输入密码"
        />
      </a-form-item>

      <!-- 提交按钮 -->
      <a-form-item>
        <a-button html-type="submit" style="width: 120px" type="primary">
          注册
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserRegisterRequest } from "../../../generated";
import { Message } from "@arco-design/web-vue";
import { useRouter } from "vue-router";

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);

const router = useRouter();

const handleSubmit = async () => {
  const res = await UserControllerService.userRegisterUsingPost(form);
  if (res.code === 0) {
    Message.success("注册成功，即将跳转登录");
    setTimeout(() => {
      router.push({ path: "/user/login", replace: true });
    }, 1000);
  } else {
    Message.error("注册失败：" + res.message);
  }
};
</script>
